package com.web.base.user.provider;

import com.blue.core.dict.State;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.user.exception.BankCardException;
import com.web.base.user.facade.BankCardFacade;
import com.web.base.user.message.BankCardRequest;
import com.web.base.user.message.BankCardResponse;
import com.web.base.user.model.BankCard;
import com.web.base.user.service.BankCardService;
import com.web.base.user.util.BankCardConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 银行卡接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月26日
 */
@Component("userBankCardProvider")
public class BankCardProvider implements BankCardFacade
{
	@Autowired
	private BankCardService bankCardService;
	@Autowired
	private StorageClient storageClient;
	
	public BankCardProvider()
	{
	}

	@Override
	public BankCardResponse add(BankCardRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		BankCard bankCard = BankCardConvertor.toBankCard(request);
		bankCard = bankCardService.save(bankCard);
		BankCardResponse response = BankCardConvertor.toResponse(bankCard, storageClient);
		return response;
	}

	@Override
	public BankCardResponse update(BankCardRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		BankCard bankCard = BankCardConvertor.toBankCard(request);
		bankCard = bankCardService.save(bankCard);
		BankCardResponse response = BankCardConvertor.toResponse(bankCard, storageClient);
		return response;
	}

	@Override
	public BankCardResponse get(Integer id)
	{
		if (id == null)
			throw new BankCardException("ID不能为空");
			
		BankCard bankCard = bankCardService.get(id);
		BankCardResponse response = BankCardConvertor.toResponse(bankCard, storageClient);
		return response;
	}

	@Override
	public List<BankCardResponse> listByUser(Integer userId, State state)
	{
		if (userId == null)
			throw new BankCardException("用户ID不能为空");

		BankCard card = new BankCard();
		card.setUserId(userId);
		card.setState(state);
		List<BankCard> list = bankCardService.list(card);
		List<BankCardResponse> responseList = BankCardConvertor.toResponse(list, storageClient);
		return responseList;
	}

	@Override
	public Page<BankCardResponse> listPage(BankCardRequest request, int start, int size)
	{
		BankCard bankCard = BankCardConvertor.toBankCard(request);
		Page<BankCard> page = new Page<>(size);
		page.setCurPage(start);
		page = bankCardService.list(bankCard, page);
		Page<BankCardResponse> pageResponse = BankCardConvertor.toResponse(page, storageClient);
		return pageResponse;
	}
	
}
